﻿using System;
using System.Data;

namespace Token.Util
{
    /// <summary>
    /// 安全数据转换帮助类
    /// by lingtianlan
    /// </summary>
    public class ConvertHelper
    {
        /// <summary>
        /// 不可实例化
        /// </summary>
        private ConvertHelper()
        {
        }

        /// <summary>
        /// 转换为string类型，若不成功，则返回空字符串
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string GetString(object o)
        {
            return GetString(o, string.Empty);
        }
        /// <summary>
        /// 转换为string类型，若不成功，则返回指定字符串
        /// </summary>
        /// <param name="o"></param>
        /// <param name="defaut"></param>
        /// <returns></returns>
        public static string GetString(object o, string defaut)
        {
            if (o != null && o.ToString() != "")
                return o.ToString();
            else
                return defaut;
        }

        /// <summary>
        /// 转换为Int类型，若不成功，返回0
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static int GetInt(object o)
        {
            return GetInt(o, 0);
        }

        /// <summary>
        /// 转换为Int类型，若不成功，则返回指定默认值
        /// </summary>
        /// <param name="o"></param>
        /// <param name="defaut"></param>
        /// <returns></returns>
        public static int GetInt(object o, int defaut)
        {
            if (o != null && o.ToString() != "")
                return Convert.ToInt32(o);
            else
                return defaut;
        }

        /// <summary>
        /// 转换为日期类型，若不成功，则返回指定的日期
        /// </summary>
        /// <param name="o"></param>
        /// <param name="defaut"></param>
        /// <returns></returns>
        public static DateTime GetDateTime(object o, DateTime defaut)
        {
            if (o != null && o.ToString() != "")
                return Convert.ToDateTime(o);
            else
                return defaut;
        }

        /// <summary>
        /// 转换为日期为型，若不成功，则返回DateTime.MinValue
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static DateTime GetDateTime(object o)
        {
            return GetDateTime(o, DateTime.MinValue);
        }

        /// <summary>
        /// 转化为特定格式的日期时间字符串
        /// </summary>
        /// <param name="format">格式字符串</param>
        /// <returns>字符串</returns>
        public static string GetFormattedTime(object o,string format,DateTime defaut)
        {
            if (o != null && o.ToString() != "")
                return Convert.ToDateTime(o).ToString(format);
            else
                return defaut.ToString(format);
        }

        /// <summary>
        /// 转化为特定格式的日期时间字符串，若日期转换不成功，则使用DateTime.MinValue取代
        /// </summary>
        /// <param name="o"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string GetFormattedTime(object o,string format)
        {
            return GetFormattedTime(o, format, DateTime.MinValue);
        }

        /// <summary>
        /// 转化为默认格式的日期时间字符串，若日期转换不成功，则使用DateTime.MinValue取代
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string GetFormattedTime(object o)
        {
            return GetFormattedTime(o, "yyyy-mm-dd hh:MM:ss", DateTime.MinValue);
        }

        /// <summary>
        /// 转化为byte类型，若不成功，则返回默认值
        /// </summary>
        /// <param name="o"></param>
        /// <param name="defaut"></param>
        /// <returns></returns>
        public static byte GetByte(object o, byte defaut)
        {
            if (o != null && o.ToString() != "")
                return Convert.ToByte(o);
            else
                return defaut;
        }

        /// <summary>
        /// 转化为byte类型，若不成功，则返回0
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static byte GetByte(object o)
        {
            return GetByte(o, 0);
        }

        /// <summary>
        /// 转化为bool类型
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static bool GetBoolean(object o)
        {
            return Convert.ToBoolean(o);
        }

        /// <summary>
        /// 转化为double类型，若不成功，则返回指定的值
        /// </summary>
        /// <param name="o"></param>
        /// <param name="defaut"></param>
        /// <returns></returns>
        public static double GetDouble(object o, double defaut)
        {
            if (o != null && o.ToString() != "")
                return Convert.ToDouble(o);
            else
                return defaut;
        }

        /// <summary>
        /// 转化为double类型，若不成功，则返回0
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static double GetDouble(object o)
        {
            return GetDouble(o, 0);
        }

        /// <summary>
        /// 转化为decimal类型，若不成功，则返回指定值
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static decimal GetDecimal(object o, decimal defaut)
        {
            if (o != null && o.ToString() != "")
                return Convert.ToDecimal(o);
            else
                return defaut;
        }

        /// <summary>
        /// 转化为decimal类型，若不成功，则返回0
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static decimal GetDecimal(object o)
        {
            return GetDecimal(o, 0);
        }
    }

}